package com.naiterui.ehp.bp.utils.scale;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.naiterui.common.redis.RedisUtil;
import com.naiterui.common.redis.template.IRedisHashTemplate;
import com.naiterui.ehp.bp.utils.encypt.AESUtil;

public class ScaleUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScaleUtil.class);
    /**
     * 量表类型：金数据
     */
    public static final String SCALETYPE_JIN = "01";
    /**
     * 量表填写来源：医生填写
     */
    public static final String SCENE_DOCTOR = "01";
    /**
     * 量表填写来源：患者填写
     */
    public static final String SCENE_PATIENT = "02";

    private static final String SCALE_KEY = "ZELskrBL1Fvm4RKw";

    public static final String REDIS_PRE_PT = "naiterui-pt|";


    /**
     * 功能描述: 生成业务id
     * 规则：量表业务id采用,两位固定标识（01:金数据）+两位填写场景（01:医生填写量表，02：患者填写量表）+时间戳+ 6位随机数
     *
     * @param: [scaleType, scene]
     * @return: String
     * @date: 2018/11/20 12:00
     * @since 1.0
     */
    public static String generateBusId(String scaleType, String scene) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(scaleType);
        stringBuffer.append(scene);
        stringBuffer.append(System.currentTimeMillis());
        stringBuffer.append((int) ((Math.random() * 9 + 1) * 100000));
        return stringBuffer.toString();
    }

    /**
     * 功能描述:从缓存中获取map数据
     * f缓存中map的key为 formId=111&doctorId=123&patientId=333&extBizId=44444&sceneId=01&formName
     *
     * @param: [key]
     * @return: Map<String, String>
     * @date: 2018/11/20 14:26
     * @since 1.0
     */
    public static Map<String, String> decryptField(String key) {
        Map<String, String> map = new HashMap<>();
        LOGGER.info("redis缓存key : {}", key);
        if (key.contains("_")) {
            LOGGER.info("从缓存中取");
            if (RedisUtil.keyOps().existsKey(key)) {
                IRedisHashTemplate redisHashTemplate = RedisUtil.hashOps();
                map = redisHashTemplate.hgetAll(key);
                LOGGER.info("redis缓存map：{}", map.toString());
            } else {
                LOGGER.info("redis缓存key不存在 : {}", key);
            }
        } else {
            LOGGER.info("解密前字符串：{}", key);
            String aesString = AESUtil.decrypt(key, "ZELskrBL1Fvm4RKw");
            LOGGER.info("解密key：{}，解密后字符串：{}", "ZELskrBL1Fvm4RKw", aesString);
            if (StringUtils.isEmpty(aesString)) {
                LOGGER.info("无法解密");
                return null;
            }
            String[] params = aesString.split("&");
            //暂定义数组长度为5
            if (params.length == 5) {
/*                for (int i= 0; i < params.length;i++) {
                    map.put(params[i].split("=")[0],params[i].split("=")[1]);
                }*/
                String formId = params[0].split("=")[1];
                String doctorId = params[1].split("=")[1];
                String patientId = params[2].split("=")[1];
                String extBizId = params[3].split("=")[1];
                String sceneId = params[4].split("=")[1].trim();
                map.put("formId", formId);
                map.put("doctorId", doctorId);
                map.put("patientId", patientId);
                map.put("extBizId", extBizId);
                map.put("sceneId", sceneId);
            }
        }
        return map;
    }

}
